package com.example.service.impl.courseServiceImpl;

import com.example.dao.courseDao.JsCourseClassDao;
import com.example.pojo.coursePojo.JsCourseClass;
import com.example.pojo.shangPinTree.Node;
import com.example.service.courseService.JsCourseClassService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author:郭
 * @Description: TODO
 * @Date: 2022/11/24 14:06
 * @ClassName JsCourseClassServiceImpl
 */
@Service
public class JsCourseClassServiceImpl implements JsCourseClassService {
    @Autowired
    private JsCourseClassDao jsCourseClassDao;
    /**
     * @Author: 郭
     * @Date: 2022/12/7 11:00
     * @Param:
     * @Return: List<Node>
     * 课程分类表中的树形，通过递归调用子节点
     */
    @Override
    public List<Node> getAll() {
        List<JsCourseClass> all = jsCourseClassDao.getAll();
        List<Node> tree = getTree(0,all);
        return tree;
    }
    @Override
    public int getCount() {
        return jsCourseClassDao.getCount();
    }
    //课程分类的树形结构
    public List<Node> getTree(int id,List<JsCourseClass> list){
        List<Node> list1 = new ArrayList<>();
        for (JsCourseClass aClass : list) {
            if (aClass.getParentId() == id){
                Node node = new Node();
                node.setId(aClass.getId());
                node.setLabel(aClass.getName());
                node.setSpread(true);
                List<Node> tree = getTree(aClass.getId(),list);
                node.setChildren(tree);
                list1.add(node);
            }
        }
        return list1;
    }
}
